#!/usr/bin/env node

const http = require('http');
const cheerio = require('cheerio');
const log = console.log;
const baseURL = 'http://www.edu2act.cn/team/21-ji-shu-ju-ku-yuan-li/'; 


function getHTMLPage() {
  return new Promise(resolve => {
      http.get(`${baseURL}`, res => {
          let html = '';
          res.on('data', chunk=> html += chunk);
          res.on('end', ()=> resolve(html));
      })
  })
} 

function getAssignmentInfo(html) {
  let $ = cheerio.load(html);
  $('div.task-list-item').each(function() {

      const taskName = $(this).find('div.f4').text().trim();
      const startTime = $(this).find('div.color-text-secondary span').eq(1).text().trim().replace(/开始于 /, '').trim();
      const stats = $(this).find('div.mt-2.mt-sm-0.ml-sm-3 span');
      const toBeVerified = $(stats[0]).text().trim().replace(/[^\d]/g, '');
      const notCompleted = $(stats[1]).text().trim().replace(/[^\d]/g, '');
      const accepted = $(stats[2]).text().trim().replace(/[^\d]/g, '');

      log(`任务名称: ${taskName}`);
      log(`开始时间: ${startTime}`);
      log(`待验证数: ${toBeVerified}`);
      log(`未完成数: ${notCompleted}`);
      log(`已验收数: ${accepted}`);
      log('---');
  });
}

async function main() {
    try {
        const html = await getHTMLPage();
        getAssignmentInfo(html);
    } catch (error) {
        console.error('Failed to fetch the page:', error.message);
        log('请检查网页链接是否正确，或稍后再试。如果问题持续存在，可能是网络问题或服务器端问题。');
    }
}

main();